package com.ciaojian.worker.config;

import com.ciaojian.core.config.CoreWebConfig;
import com.ciaojian.core.interceptor.ApiIdempotentInterceptor;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.dreamyoung.mprelation.AutoMapper;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author Atlakyn
 */
@Configuration
public class MvcConfig extends CoreWebConfig {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiIdempotentInterceptor()).addPathPatterns("/worker/**");
    }

    @Bean
    ApiIdempotentInterceptor apiIdempotentInterceptor() {
        return new ApiIdempotentInterceptor();
    }

    @Bean
    public AutoMapper autoMapper() {
        return new AutoMapper(new String[]{
                "com.ciaojian.core.model"
        });
    }
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            builder.locale(Locale.CHINA);
            builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
            //转换时间格式
            builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //转换定义的工具类
            builder.modules(new JacksonLocalDateTimeModule());
            //为null的数据不返回给前端
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        };
    }
}
